//
//  LBXTrackingAreasView.m
//  MacDevTest
//
//  Created by lbxia on 2020/8/16.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "LBXTrackingAreasView.h"
@interface LBXTrackingAreasView()
@property (nonatomic, strong) NSTrackingArea *trackingArea;
@end

@implementation LBXTrackingAreasView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    
    // Drawing code here.
    
    [self removeCustomTrackingArea];
    
    self.trackingArea = [[NSTrackingArea alloc] initWithRect:dirtyRect options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved |
                         NSTrackingCursorUpdate |
                         NSTrackingActiveWhenFirstResponder |
                         NSTrackingActiveInKeyWindow |
                         NSTrackingActiveInActiveApp |
                         NSTrackingActiveAlways |
                         NSTrackingAssumeInside |
                         NSTrackingInVisibleRect |
                         NSTrackingEnabledDuringMouseDrag
                                                       owner:self userInfo:nil];
    [self addTrackingArea:self.trackingArea];
       
    [self becomeFirstResponder];
}

- (void)dealloc
{
    [self removeCustomTrackingArea];
}

- (void)removeCustomTrackingArea
{
    if(self.trackingArea) {
        [self removeTrackingArea:self.trackingArea];
        self.trackingArea = nil;
        
        [self resignFirstResponder];
    }
}

- (void)updateTrackingAreas {
    
    [super updateTrackingAreas];
    
//    NSTrackingAreaOptions options = NSTrackingInVisibleRect|NSTrackingMouseEnteredAndExited|NSTrackingActiveAlways;
//    self.trackingArea = [[NSTrackingArea alloc] initWithRect:CGRectZero options:options owner:self userInfo:nil];
//     [self addTrackingArea:self.trackingArea];
    
   
}

- (void)mouseEntered:(NSEvent *)theEvent {
    
//    NSLog(@"mouseEntered");
//    [super mouseEntered:theEvent];
    
    if (_delegate && [_delegate respondsToSelector:@selector(mouseEnteredEventWithView:)]) {
        [_delegate mouseEnteredEventWithView:self];
    }
    else
    {
        [super mouseEntered:theEvent];
    }
}

- (void)mouseExited:(NSEvent *)theEvent {
    
//    [super mouseEntered:theEvent];
    
    if (_delegate && [_delegate respondsToSelector:@selector(mouseEnteredEventWithView:)]) {
        [_delegate mouseExitedEventWithView:self];
    }
    else
    {
        [super mouseEntered:theEvent];
    }
}

- (void)rightMouseDown:(NSEvent *)event
{
    NSLog(@"rightMouseDown");
    self.layer.borderColor = [NSColor blueColor].CGColor;
    self.layer.borderWidth = 1;
    
    if (_delegate && [_delegate respondsToSelector:@selector(mouseRightDownEventWithView:)]) {
        [_delegate mouseRightDownEventWithView:self];
    }else
    {
        [super mouseDown:event];
    }
}


- (void)mouseDown:(NSEvent *)event {

    //需要调用下面改行代码，否则NSCollectionView didselect事件触发不了
    if (_delegate && [_delegate respondsToSelector:@selector(mouseLeftDownEventWithView:)]) {
        [_delegate mouseLeftDownEventWithView:self];
    }else
    {
        
        [super mouseDown:event];
    }
    
    if (event.type == NSEventTypeLeftMouseDown) {
        NSLog(@"mouseLeftDown");
        
        self.layer.borderColor = [NSColor clearColor].CGColor;

        return;
//        self.layer.backgroundColor = [NSColor lightGrayColor].CGColor;
    }

    NSLog(@"mouseDown");
    
}

- (void)mouseUp:(NSEvent *)event {

    [super mouseUp:event];
//     NSLog(@"mouseUp");
}

@end
